package org.itboys.core.service.mongo.web;

import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.entity.mongo.SundayCompanyInfo;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Auther: 罗尧坤
 * Date: 2018/8/22
 * Description:
 */
@Service
public class SundayCompanyInfoService extends BaseMongoService<SundayCompanyInfo> {
    @Resource(name = "mobileDs")
    private MongoDataSource mobileDataSource;

    @Override
    protected MongoDataSource getMongoDataSource() {
        return mobileDataSource;
    }

    @Override
    protected Class<SundayCompanyInfo> getEntityClass() {
        return SundayCompanyInfo.class;
    }

    /**
     * 分页查询企业信息
     *
     * @param request
     * @return
     */
    public PageResult<SundayCompanyInfo> select(HttpServletRequest request) {
        Map<String, Object> param = ParamUtil.packageMongoExactParam(request);
        param.put("isDeleted", SundayCommonConstants.NO);

        return super.pageQuery(request, param);
    }

    /**
     * 保存或者新增
     *
     * @param id
     * @param info
     */
    public void save(Long id, SundayCompanyInfo info) {
        if (LongUtil.isNotZreo(id)) {
            super.updateExceptEmpty(id, info);
        } else {
            if (super.count(new HashMap<>()) > 0) {
                throw new ErrorException("已经存在一个企业信息");
            }else super.save(info);
        }
    }

    /**
     * 查找单个
     *
     * @param id
     * @return
     */
    public SundayCompanyInfo findOne(Long id) {
        SundayCompanyInfo info = new SundayCompanyInfo();
        if (LongUtil.isNotZreo(id)) {
            info = super.getById(id);
            return info;
        } else {
            return info;
        }
    }

}
